<template>
    <div class="page login-view">
        <wv-header title="用户登录" fixed>
            <div class="btn-menu" slot="right">
                <span class="register-btn" @click="$router.push({name: 'register'})">注册</span>
            </div>
        </wv-header>
        <div class="main-header-fixed">
            <div class="login-wrapper">
                <wv-group >
                    <wv-input label="手机号码" placeholder="用户名/手机号码" v-model="fields.username"></wv-input>
                    <wv-input class="password-input" label="密 码" type="password" placeholder="请输入密码" v-model="fields.password"></wv-input>
                </wv-group>
                <wv-button type="primary" @click="login">登录</wv-button>
                <a @click="forgotPassword" style="margin-top: 15px; color: #5191ff;float:right;">忘记密码?</a>
            </div>
        </div>
    </div>
</template>

<script>
    import util from '@/libs/util';
    import storage from '@/libs/storage';
    import config from '@/config/';
    export default {
        data () {
            return {
                fields: {
                    username: '',
                    password: ''
                }
            }
        },

        methods: {
            forgotPassword () {
                this.$router.push({ name: 'reset-password-mobile' });
            },

            login () {
                let that = this;
                /**
                 * 开始授权登录
                 */
                var form_params = {
                    'username': this.fields.username,
                    'password': this.fields.password,
                };

                util.ajax.post('/api/user/login', form_params).then((resp) => {
                    storage.set('tokens', resp.data);

                    that.$store.dispatch('setTokens', resp.data);

                    util.api().get('/api/user').then((resp) => {
                        if (resp.data.status == true) {
                            this.$root.success('登录成功');

                            that.$store.dispatch('setUser', resp.data.data);

                            if (this.$store.state.app.backUrl.length > 0) {
                                this.$router.push({
                                    path: this.$store.state.app.backUrl
                                });
                            } else {
                                // 登录成功进入首页
                                this.$router.push({
                                    name: 'user-profiles'
                                });
                            }
                        } else {
                            this.$root.error(resp.data.message);
                        }
                    }).catch((e) => {
                        if (e.response) {
                            this.$root.topTips(resp.data.message);
                        } else {
                            this.$root.topTips('索取用户信息失败，请求发送失败');
                        }
                    });
                }).catch((e) => {
                    if (e.response) {
                        this.$root.topTips('登录失败');
                    } else {
                        this.$root.topTips('登录失败,请求错误');
                    }
                });
            }
        }
    }
</script>

<style lang="scss">
    .weui-cell.password-input .weui-label {
        letter-spacing: 14px;
    }
</style>